package task3

import grails.test.mixin.Mock
import grails.test.mixin.TestFor

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Human)
@Mock([Project, Department])
class HumanTests {

    void testConstrains() {

        def dep_1 = new Department(name: "Department1")
        dep_1.save()


        def proj_1=new Project(name:"Project1", department: dep_1)
        proj_1.save()

        mockForConstraintsTests(Human)
        def human1 = new Human(name: "", position: "",project:proj_1)
        assertFalse human1.validate()
        assertEquals 2, human1.errors.errorCount
        assertEquals "blank", human1.errors["name"]
        assertEquals "blank", human1.errors["position"]

        human1.name = "Ed"
        human1.position="pr"
        human1.age =12

        assertFalse human1.validate()
        assertEquals "size", human1.errors["name"]
        assertEquals "size", human1.errors["position"]
        assertEquals "min",human1.errors["age"]

        human1.name = "E"*21
        human1.position="p"*21
        human1.age =100
        human1.humanEmail="fklakldf"

        assertFalse human1.validate()
        assertEquals "size", human1.errors["name"]
        assertEquals "size", human1.errors["position"]
        assertEquals "max",human1.errors["age"]
        assertEquals "email",human1.errors["humanEmail"]

        human1.name = "Edward"
        human1.position="programmer"
        human1.age =45
        human1.humanEmail="edward@mai.pu"

        assertTrue human1.validate()
        assertEquals 0, Human.list().size()

        human1.save()
        assertEquals 1, Human.list().size()

        def human2 = new Human(name:"Edward",position:"programmer",project: proj_1)
        assertFalse human2.validate()
        assertEquals "unique",human2.errors["name"]


        def proj_2=new Project(name:"Project2", department: dep_1)
        proj_2.save()

        proj_2.addToPeople(human2)
        assertTrue human2.validate()
        human2.save()

        assertEquals 2, Human.list().size()

        def list = Human.findAllByPosition("programmer")
        assertEquals 2,list.size()

    }
}
